package com.pig.window;

import java.awt.Container;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import com.pig.util.ContinuousMusic;
import com.pig.util.CutImage;

public class LoginFrame extends JFrame {

	private static final long serialVersionUID = 1L;

	Image login;

	Image[] menus;

	Icon[] menusImg;

	JLabel jlBk;

	JLabel jlStart;

	JLabel jlOptions;

	JLabel jlHelp;

	JLabel jlExit;

	Container con;

	private final int FWIDTH = 800;

	private final int FHEIGHT = 600;

	int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;

	int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;

	ContinuousMusic loginMusic;

	public LoginFrame() {
		loginMusic = new ContinuousMusic("music/bkLog.wav");
		loginMusic.play();
		con = this.getContentPane();
		con.setLayout(null);

		CutImage ci = new CutImage();
		this.setSize(FWIDTH, FHEIGHT);
		this.setLocation((screenWidth - FWIDTH) / 2,
				(screenHeight - FHEIGHT) / 2);

		login = ci.getImage("images/login.jpg");
		menus = ci.getMenus();
		menusImg = new Icon[menus.length];

		Icon loginImg = new ImageIcon(login);
		for (int i = 0; i < menus.length; i++) {
			menusImg[i] = new ImageIcon(menus[i]);
		}
		jlBk = new JLabel(loginImg);
		jlStart = new JLabel(menusImg[0]);
		jlOptions = new JLabel(menusImg[2]);
		jlHelp = new JLabel(menusImg[4]);
		jlExit = new JLabel(menusImg[6]);

		jlBk.setBounds(0, 0, FWIDTH, FHEIGHT);
		jlStart.setBounds(100, 160, 200, 100);
		jlOptions.setBounds(100, 260, 200, 100);
		jlHelp.setBounds(100, 360, 200, 100);
		jlExit.setBounds(100, 460, 200, 100);

		jlStart.addMouseListener(new MouseAdapter(){

			@Override
			public void mouseEntered(MouseEvent e) {
				jlStart.setIcon(menusImg[1]);
			}

			@Override
			public void mouseExited(MouseEvent e) {
				jlStart.setIcon(menusImg[0]);
			}

			@Override
			public void mouseClicked(MouseEvent e) {
				
				//************************
				loginMusic.stop();
				dispose();
			}
			
		});
		
		jlOptions.addMouseListener(new MouseAdapter(){

			@Override
			public void mouseEntered(MouseEvent e) {
				jlOptions.setIcon(menusImg[3]);
			}

			@Override
			public void mouseExited(MouseEvent e) {
				jlOptions.setIcon(menusImg[2]);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				//*****************
				loginMusic.stop();
				dispose();
			}

		});
		
		jlHelp.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseEntered(MouseEvent e) 
			{
				jlHelp.setIcon(menusImg[5]);
			}
			@Override
			public void mouseExited(MouseEvent e) 
			{
				jlHelp.setIcon(menusImg[4]);
			}
			@Override
			public void mouseClicked(MouseEvent e) 
			{
				//************************
				loginMusic.stop();
				dispose();
			}
		}
		);
		
		jlExit.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseEntered(MouseEvent e) 
			{
				jlExit.setIcon(menusImg[7]);
			}
			@Override
			public void mouseExited(MouseEvent e) 
			{
				jlExit.setIcon(menusImg[6]);
			}
			@Override
			public void mouseClicked(MouseEvent e) 
			{
				loginMusic.stop();
				System.exit(0);
			}
		}
		);
		
		jlBk.add(jlStart);
		jlBk.add(jlOptions);
		jlBk.add(jlHelp);
		jlBk.add(jlExit);
		con.add(jlBk);

		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new LoginFrame();
	}
}
